<?php
/**
 * Created by PhpStorm.
 * User: wanglong
 * Date: 2018/1/5
 * Time: 21:11
 */

namespace app\modules\weixin\controllers;


use app\modules\m\controllers\common\BaseController;

class MsgController extends BaseController
{
    /**
     * 处理微信等方面的信息
     */
    public function actionIndex()
    {
        if(!$this->CheckSignature()){
            return "验证失败";
        }

        if(array_key_exists("echostr",$_GET) && $_GET['echostr']){//用于微信第一次验证
            return $_GET['nonce'];
        }
    }

    //验证微信签名
    public function CheckSignature()
    {
        //获取微信加密签名
        $signature=trim($this->get("signature",""));
        //获取时间戳
        $timestamp=trim($this->get("timestamp",""));
        //获取随机数
        $nonce=trim($this->get("nonce",""));
        //将三者放到一个数组 并进行字典排序
        $tmpArr=array(\Yii::$app->params["weixin"]['token'],$timestamp,$nonce);
        sort($tmpArr);

        //将数组拼接成字符串
        $tmpStr=implode($tmpArr);
        //对拼接的字符串进行加密
        $tmpStr=sha1($tmpStr);
        //判断加密签名是否与拼接后的字符串相等
        if( $tmpStr == $signature){
            return true;
        }else{
            return false;
        }
    }
}